package com.yanqu.road.dao.impl.tradewar;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.tradewar.TradeWarActivityJoinUnion;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TradeWarActivityJoinUnionDaoImpl extends TempDataObjectDao<TradeWarActivityJoinUnion> {

    public Map<Integer,Map<Integer,TradeWarActivityJoinUnion>> getTradeWarActivityJoinUnionMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_tradewar_activity_union";
        Map<Integer,Map<Integer,TradeWarActivityJoinUnion>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                TradeWarActivityJoinUnion data = parse(rs);
                if (dataMap.get(data.getActivityId()) == null) {
                    dataMap.put(data.getActivityId(), new ConcurrentHashMap<>());
                }
                Map<Integer, TradeWarActivityJoinUnion> groupMap = dataMap.get(data.getActivityId());
                groupMap.put(data.getGroupId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(TradeWarActivityJoinUnion data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_tradewar_activity_union(activityId,groupId,joinUnion) values(?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getActivityId());
            call.setInt(i++, data.getGroupId());
            call.setString(i++, StringUtils.listToString(data.getUnionUidList(), ","));
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(TradeWarActivityJoinUnion data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_tradewar_activity_union SET joinUnion=? where activityId=? and groupId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, StringUtils.listToString(data.getUnionUidList(), ","));
            call.setInt(i++, data.getActivityId());
            call.setInt(i++, data.getGroupId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected TradeWarActivityJoinUnion parseData(ResultSet rs) throws SQLException {
        TradeWarActivityJoinUnion data = new TradeWarActivityJoinUnion();
        data.setActivityId(rs.getInt("activityId"));
        data.setGroupId(rs.getInt("groupId"));
        String value = rs.getString("joinUnion");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setUnionUidList(StringUtils.stringToStringList(value, ","));
        }
        return data;
    }
}
